/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ComGLHoodIView.java
*
* Created on November 15, 2007, 4:36 PM
 */
package gui.genomemap.view.inputView;

import genomemap.worker.LHood;

import gui.genomemap.event.event.WorkerInputEvent;
import gui.genomemap.inputModel.AnnealIModel;
import gui.genomemap.popup.UsedOrderPopup;
import gui.genomemap.view.inputView.options.AnnealIOptions;

import javautil.swing.SwingUtil;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

/**
 *
 * @author  stewari1
 */
public class AnnealIView extends InputView {

    /** Field description */
    private final AnnealIOptions optionsDialog = new AnnealIOptions(new javax.swing.JFrame(),
                                                     false);

    /** Field description */
    private JPopupMenu tablePopUp = null;


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JCheckBox embedSeq_JCheckBox;

    /** Field description */
    private javax.swing.JLabel jLabel1;

    /** Field description */
    private javax.swing.JLabel jLabel2;

    /** Field description */
    private javax.swing.JComboBox lHoodType_JComboBox;

    /** Field description */
    private javax.swing.ButtonGroup lHoodType_buttonGroup;

    /** Field description */
    private javax.swing.JLabel linkageGrp_JLabel;

    /** Field description */
    private javax.swing.JComboBox linkagegrp_JComboBox;

    /** Field description */
    private javax.swing.JButton options_JButton;

    /** Field description */
    private javax.swing.JTextField order_JTextField;

    /** Field description */
    private javax.swing.JButton run_JButton;

    /** Field description */
    private javax.swing.JCheckBox usedOrder_JCheckBox;

    /** Creates new form ComGLHoodIView */
    public AnnealIView() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lHoodType_buttonGroup = new javax.swing.ButtonGroup();
        run_JButton           = new javax.swing.JButton();
        options_JButton       = new javax.swing.JButton();
        linkageGrp_JLabel     = new javax.swing.JLabel();
        linkagegrp_JComboBox  = new javax.swing.JComboBox();
        jLabel1               = new javax.swing.JLabel();
        order_JTextField      = new javax.swing.JTextField();
        usedOrder_JCheckBox   = new javax.swing.JCheckBox();
        jLabel2               = new javax.swing.JLabel();
        lHoodType_JComboBox   = new javax.swing.JComboBox();
        embedSeq_JCheckBox    = new javax.swing.JCheckBox();

        run_JButton.setText("Run");
        run_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                run_JButtonActionPerformed(evt);
            }

        });
        options_JButton.setText("Options");
        options_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                options_JButtonActionPerformed(evt);
            }

        });
        linkageGrp_JLabel.setText("Linkage Group");
        linkagegrp_JComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "1", "2", "3", "4", "5", "6", "7"
        }));
        jLabel1.setText("Enter Starting Order");
        order_JTextField.setToolTipText("Enter \",\" seperated natural numbers");
        usedOrder_JCheckBox.setText("Choose Used Orders ?");
        usedOrder_JCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        usedOrder_JCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        usedOrder_JCheckBox.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usedOrder_JCheckBoxActionPerformed(evt);
            }

        });
        jLabel2.setText("LHood Type");
        lHoodType_JComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "Genetic Likelihood",
            "Physical Likelihood", "Joint Likelihood" }));
        embedSeq_JCheckBox.setText("Embed Sequence");
        embedSeq_JCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup().addGap(19, 19, 19)
                        .addGroup(layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(linkageGrp_JLabel).addComponent(jLabel1)
                            .addComponent(jLabel2)).addGap(16, 16, 16)
                                .addGroup(layout
                                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(linkagegrp_JComboBox,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(order_JTextField,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 160,
                                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(lHoodType_JComboBox,
                                                            javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                130,
                                                                javax.swing.GroupLayout
                                                                    .PREFERRED_SIZE))
                                                                        .addGap(60, 60, 60)
                                                                            .addGroup(layout
                                                                                .createParallelGroup(javax
                                                                                    .swing.GroupLayout
                                                                                        .Alignment
                                                                                            .LEADING)
                                                                                                .addComponent(embedSeq_JCheckBox)
                                                                                                    .addComponent(usedOrder_JCheckBox)))
                                                                                                        .addGroup(layout
                                                                                                            .createSequentialGroup()
                                                                                                                .addGap(353,
                                                                                                                    353,
                                                                                                                    353)
                                                                                                                    .addComponent(options_JButton)
                                                                                                                        .addPreferredGap(javax
                                                                                                                            .swing.LayoutStyle
                                                                                                                                .ComponentPlacement
                                                                                                                                    .RELATED)
                                                                                                                                        .addComponent(run_JButton)))
                                                                                                                                            .addContainerGap(32,
                                                                                                                                                Short.MAX_VALUE)));
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        layout.createParallelGroup(
                            javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                            linkagegrp_JComboBox, javax.swing.GroupLayout.PREFERRED_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                linkageGrp_JLabel)).addGroup(
                                    layout.createParallelGroup(
                                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                        options_JButton).addComponent(run_JButton))).addGap(
                                            13, 13, 13).addGroup(
                                                layout.createParallelGroup(
                                                    javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                        jLabel1).addComponent(
                                                            order_JTextField,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                    javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
                                                                            usedOrder_JCheckBox)).addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(
                                                                                    layout.createParallelGroup(
                                                                                        javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                                                                            jLabel2).addComponent(
                                                                                                embedSeq_JCheckBox).addComponent(
                                                                                                    lHoodType_JComboBox,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                            javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap(
                                                                                                                    27,
                                                                                                                    Short.MAX_VALUE)));

    }    // </editor-fold>//GEN-END:initComponents

    /**
     * Method description
     *
     * @param evt description
     */
    private void usedOrder_JCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_usedOrder_JCheckBoxActionPerformed

        if (!usedOrder_JCheckBox.isSelected()) {

            System.out.println("selected not");

            if (tablePopUp != null) {
                tablePopUp.setVisible(false);
            }

        } else {

            System.out.println("selected");

            if (tablePopUp == null) {

                tablePopUp = UsedOrderPopup.createPopup(getView());


                // add popup menu listener
                tablePopUp.addPopupMenuListener(new PopupMenuListener() {

                    public void popupMenuCanceled(PopupMenuEvent e) {}
                    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                        usedOrder_JCheckBox.setSelected(false);
                    }
                    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}

                });


                // add cell double click listener to the table under the popup first time
                final JTable table = (JTable) tablePopUp.getClientProperty(UsedOrderPopup.TABLE);

                table.addMouseListener(new MouseAdapter() {

                    public void mousePressed(MouseEvent e) {

                        int x = e.getX();
                        int y = e.getY();

                        if (e.getClickCount() == 2) {

                            int col = table.getSelectedColumn();

                            if (col != 2) {
                                return;
                            }

                            int row = table.getSelectedRow();


                            // set the ch id
                            linkagegrp_JComboBox.setSelectedItem("" + table.getValueAt(row, 0));


                            // set the order string
                            AnnealIView.this.setOrderString("" + table.getValueAt(row, col));
                        }
                    }

                });
            }

            tablePopUp.show(this, 100, 90);
        }

    }    // GEN-LAST:event_usedOrder_JCheckBoxActionPerformed

    String getOrderString() {
        return order_JTextField.getText();
    }

    public void setOrderString(String orderString) {
        order_JTextField.setText(orderString);
    }

    /**
     * Method description
     *
     * @param evt description
     */
    private void options_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_options_JButtonActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                optionsDialog.setLocationRelativeTo(null);
                optionsDialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_options_JButtonActionPerformed

    @Override
    public void referesh() {

        if (getInputModel() == null) {

            setEnabledRunModel(true);
            linkagegrp_JComboBox.setSelectedItem("1");
            setOrderString("");

            return;
        }

        AnnealIModel annealIModel = (AnnealIModel) getInputModel();

        setOrder(annealIModel.getOrderSet());
        linkagegrp_JComboBox.setSelectedItem("" + annealIModel.getLinkageGroup());
        optionsDialog.setOptions(annealIModel);
    }

    /**
     * Method description
     *
     * @param evt description
     */
    private void run_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_run_JButtonActionPerformed

        AnnealIModel inputModel = new AnnealIModel();


        // get linkage group
        inputModel.setLinkageGroup(
            Integer.parseInt((String) linkagegrp_JComboBox.getSelectedItem()));


        // set the lhoodType
        int index = lHoodType_JComboBox.getSelectedIndex();

        if (index == 0) {
            inputModel.setLhoodType(LHood.GLHOOD);
        }

        if (index == 1) {
            inputModel.setLhoodType(LHood.PLHOOD);
        }

        if (index == 2) {
            inputModel.setLhoodType(LHood.JLHOOD);
        }


        // get order
        String orderString = getOrderString();

        if (orderString.length() == 0) {

            SwingUtil.showErrorDialog(this, "The order text field is empty !");

            return;
        }

        String[] orders = orderString.split(",");


        // check if ghood is chosen as LHood type
        if ((inputModel.getLhoodType() == LHood.GLHOOD) && (orders.length % 2 == 0)) {

            SwingUtil.showErrorDialog(this, "The number of markers should be odd");

            return;
        }

        for (String val : orders) {

            inputModel.addOrder(Integer.parseInt(val));
        }


        // get other options from the options dialog
        optionsDialog.getOptions(inputModel);

        WorkerInputEvent runLHoodEvent = new WorkerInputEvent(this, WorkerInputEvent.ANNEAL);

        runLHoodEvent.setInputModel(inputModel);
        fireWorkerInputEvent(runLHoodEvent);

    }    // GEN-LAST:event_run_JButtonActionPerformed

    @Override
    public void setEnabledRunModel(boolean enabled) {
        run_JButton.setEnabled(enabled);
    }


    // End of variables declaration//GEN-END:variables
}
